<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Type Conversion Macros</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2">
<link rel="start" href="index.html" title="GLib Reference Manual">
<link rel="up" href="glib-fundamentals.html" title="GLib Fundamentals">
<link rel="prev" href="glib-Standard-Macros.html" title="Standard Macros">
<link rel="next" href="glib-Byte-Order-Macros.html" title="Byte Order Macros">
<meta name="generator" content="GTK-Doc V1.9 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="chapter" href="glib.html" title="GLib Overview">
<link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals">
<link rel="chapter" href="glib-core.html" title="GLib Core Application Support">
<link rel="chapter" href="glib-utilities.html" title="GLib Utilities">
<link rel="chapter" href="glib-data-types.html" title="GLib Data Types">
<link rel="chapter" href="tools.html" title="GLib Tools">
<link rel="index" href="ix01.html" title="Index">
<link rel="index" href="ix02.html" title="Index of deprecated symbols">
<link rel="index" href="ix03.html" title="Index of new symbols in 2.2">
<link rel="index" href="ix04.html" title="Index of new symbols in 2.4">
<link rel="index" href="ix05.html" title="Index of new symbols in 2.6">
<link rel="index" href="ix06.html" title="Index of new symbols in 2.8">
<link rel="index" href="ix07.html" title="Index of new symbols in 2.10">
<link rel="index" href="ix08.html" title="Index of new symbols in 2.12">
<link rel="index" href="ix09.html" title="Index of new symbols in 2.14">
<link rel="index" href="ix10.html" title="Index of new symbols in 2.16">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2">
<tr valign="middle">
<td><a accesskey="p" href="glib-Standard-Macros.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td><a accesskey="u" href="glib-fundamentals.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">GLib Reference Manual</th>
<td><a accesskey="n" href="glib-Byte-Order-Macros.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr>
<tr><td colspan="5" class="shortcuts"><nobr><a href="#id2889320" class="shortcut">Top</a>
                  &#160;|&#160;
                  <a href="#id2889390" class="shortcut">Description</a></nobr></td></tr>
</table>
<div class="refentry" lang="en">
<a name="glib-Type-Conversion-Macros"></a><div class="titlepage"></div>
<div class="refnamediv"><table width="100%"><tr>
<td valign="top">
<h2>
<a name="id2889320"></a><span class="refentrytitle">Type Conversion Macros</span>
</h2>
<p>Type Conversion Macros &#8212; portably storing integers in pointer variables</p>
</td>
<td valign="top" align="right"></td>
</tr></table></div>
<div class="refsynopsisdiv">
<h2>Synopsis</h2>
<pre class="synopsis">

#include &lt;glib.h&gt;


#define             <a class="link" href="glib-Type-Conversion-Macros.html#GINT-TO-POINTER:CAPS">GINT_TO_POINTER</a>                     (i)
#define             <a class="link" href="glib-Type-Conversion-Macros.html#GPOINTER-TO-INT:CAPS">GPOINTER_TO_INT</a>                     (p)

#define             <a class="link" href="glib-Type-Conversion-Macros.html#GUINT-TO-POINTER:CAPS">GUINT_TO_POINTER</a>                    (u)
#define             <a class="link" href="glib-Type-Conversion-Macros.html#GPOINTER-TO-UINT:CAPS">GPOINTER_TO_UINT</a>                    (p)
#define             <a class="link" href="glib-Type-Conversion-Macros.html#GSIZE-TO-POINTER:CAPS">GSIZE_TO_POINTER</a>                    (s)
#define             <a class="link" href="glib-Type-Conversion-Macros.html#GPOINTER-TO-SIZE:CAPS">GPOINTER_TO_SIZE</a>                    (p)
</pre>
</div>
<div class="refsect1" lang="en">
<a name="id2889390"></a><h2>Description</h2>
<p>
Many times GLib, GTK+, and other libraries allow you to pass "user
data" to a callback, in the form of a void pointer. From time to time
you want to pass an integer instead of a pointer. You could allocate
an integer, with something like:
</p>
<div class="informalexample"><pre class="programlisting">
 int *ip = g_new (int, 1);
 *ip = 42;
</pre></div>
<p>
But this is inconvenient, and it's annoying to have to free the 
memory at some later time.
</p>
<p>
Pointers are always at least 32 bits in size (on all platforms GLib
intends to support). Thus you can store at least 32-bit integer values
in a pointer value. Naively, you might try this, but it's incorrect:
</p>
<div class="informalexample"><pre class="programlisting">
 gpointer p;
 int i;
 p = (void*) 42;
 i = (int) p;
</pre></div>
<p>
Again, that example was <span class="emphasis"><em>not</em></span> correct, don't copy it. 
The problem is that on some systems you need to do this:
</p>
<div class="informalexample"><pre class="programlisting">
 gpointer p;
 int i;
 p = (void*) (long) 42;
 i = (int) (long) p;
</pre></div>
<p>
So <a class="link" href="glib-Type-Conversion-Macros.html#GPOINTER-TO-INT:CAPS"><code class="function">GPOINTER_TO_INT()</code></a>, <a class="link" href="glib-Type-Conversion-Macros.html#GINT-TO-POINTER:CAPS"><code class="function">GINT_TO_POINTER()</code></a>, etc. do the right thing
on the current platform.
</p>
<p>
</p>
<div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Warning</h3>
<p>
YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS NOT PORTABLE IN ANY
WAY SHAPE OR FORM. These macros <span class="emphasis"><em>ONLY</em></span> allow
storing integers in pointers, and only preserve 32 bits of the
integer; values outside the range of a 32-bit integer will be mangled.
</p>
</div>
<p>
</p>
</div>
<div class="refsect1" lang="en">
<a name="id2889486"></a><h2>Details</h2>
<div class="refsect2" lang="en">
<a name="id2889496"></a><h3>
<a name="GINT-TO-POINTER:CAPS"></a>GINT_TO_POINTER()</h3>
<a class="indexterm" name="id2889509"></a><pre class="programlisting">#define GINT_TO_POINTER(i)	((gpointer)  (i))
</pre>
<p>
Stuffs an integer into a pointer type.
</p>
<p>
Remember, YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS NOT PORTABLE
IN ANY WAY SHAPE OR FORM. These macros <span class="emphasis"><em>ONLY</em></span> allow
storing integers in pointers, and only preserve 32 bits of the
integer; values outside the range of a 32-bit integer will be mangled.
</p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code>i</code></em>&#160;:</span></p></td>
<td>integer to stuff into a pointer.


</td>
</tr></tbody>
</table></div>
</div>
<hr>
<div class="refsect2" lang="en">
<a name="id2889555"></a><h3>
<a name="GPOINTER-TO-INT:CAPS"></a>GPOINTER_TO_INT()</h3>
<a class="indexterm" name="id2889568"></a><pre class="programlisting">#define GPOINTER_TO_INT(p)	((gint)   (p))
</pre>
<p>
Extracts an integer from a pointer. The integer must have
been stored in the pointer with <a class="link" href="glib-Type-Conversion-Macros.html#GINT-TO-POINTER:CAPS"><code class="function">GINT_TO_POINTER()</code></a>.
</p>
<p>
Remember, YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS NOT PORTABLE
IN ANY WAY SHAPE OR FORM. These macros <span class="emphasis"><em>ONLY</em></span> allow
storing integers in pointers, and only preserve 32 bits of the
integer; values outside the range of a 32-bit integer will be mangled.
</p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code>p</code></em>&#160;:</span></p></td>
<td>pointer containing an integer.


</td>
</tr></tbody>
</table></div>
</div>
<hr>
<div class="refsect2" lang="en">
<a name="id2889626"></a><h3>
<a name="GUINT-TO-POINTER:CAPS"></a>GUINT_TO_POINTER()</h3>
<a class="indexterm" name="id2889639"></a><pre class="programlisting">#define GUINT_TO_POINTER(u)	((gpointer)  (u))
</pre>
<p>
Stuffs an unsigned integer into a pointer type.
</p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code>u</code></em>&#160;:</span></p></td>
<td>unsigned integer to stuff into the pointer.


</td>
</tr></tbody>
</table></div>
</div>
<hr>
<div class="refsect2" lang="en">
<a name="id2889674"></a><h3>
<a name="GPOINTER-TO-UINT:CAPS"></a>GPOINTER_TO_UINT()</h3>
<a class="indexterm" name="id2889687"></a><pre class="programlisting">#define GPOINTER_TO_UINT(p)	((guint)  (p))
</pre>
<p>
Extracts an unsigned integer from a pointer. The integer must have
been stored in the pointer with <a class="link" href="glib-Type-Conversion-Macros.html#GUINT-TO-POINTER:CAPS"><code class="function">GUINT_TO_POINTER()</code></a>.
</p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code>p</code></em>&#160;:</span></p></td>
<td>pointer to extract an unsigned integer from.


</td>
</tr></tbody>
</table></div>
</div>
<hr>
<div class="refsect2" lang="en">
<a name="id2889734"></a><h3>
<a name="GSIZE-TO-POINTER:CAPS"></a>GSIZE_TO_POINTER()</h3>
<a class="indexterm" name="id2889746"></a><pre class="programlisting">#define GSIZE_TO_POINTER(s)	((gpointer) (gsize) (s))
</pre>
<p>
Stuffs a <a class="link" href="glib-Basic-Types.html#gsize"><span class="type">gsize</span></a> into a pointer type.
</p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code>s</code></em>&#160;:</span></p></td>
<td>
<a class="link" href="glib-Basic-Types.html#gsize"><span class="type">gsize</span></a> to stuff into the pointer.


</td>
</tr></tbody>
</table></div>
</div>
<hr>
<div class="refsect2" lang="en">
<a name="id2889795"></a><h3>
<a name="GPOINTER-TO-SIZE:CAPS"></a>GPOINTER_TO_SIZE()</h3>
<a class="indexterm" name="id2889809"></a><pre class="programlisting">#define GPOINTER_TO_SIZE(p)	((gsize) (p))
</pre>
<p>
Extracts a <a class="link" href="glib-Basic-Types.html#gsize"><span class="type">gsize</span></a> from a pointer. The <a class="link" href="glib-Basic-Types.html#gsize"><span class="type">gsize</span></a> must have
been stored in the pointer with <a class="link" href="glib-Type-Conversion-Macros.html#GSIZE-TO-POINTER:CAPS"><code class="function">GSIZE_TO_POINTER()</code></a>.
</p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code>p</code></em>&#160;:</span></p></td>
<td>pointer to extract a <a class="link" href="glib-Basic-Types.html#gsize"><span class="type">gsize</span></a> from.


</td>
</tr></tbody>
</table></div>
</div>
</div>
<div class="refsect1" lang="en">
<a name="id2889879"></a><div class="refsect2" lang="en"><a name="id2889880"></a></div>
<hr>
<div class="refsect2" lang="en"><a name="id2889881"></a></div>
</div>
</div>
</body>
</html>
